package com.thinklib.controller;

import com.thinklib.bean.TestBean;
import com.thinklib.mapper.UserMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 插入100万测试数据
 */
@RestController
@RequestMapping("/api/tst/")
public class TestController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private TestBean testBean;

    @GetMapping("add_test")
    public void addTest() {
        ExecutorService executorService = Executors.newFixedThreadPool(40);
        ReentrantLock lock = new ReentrantLock();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    lock.lock();
                    for (int i = 0; i < 1000000; i++) {
                        testBean.setUsername("zhangsan：" + i);
                        testBean.setPassword("pwd：" + i);
                        testBean.setRemark("测试数据" + i);
                        userMapper.addTest(testBean);
                    }
                }catch (Exception e) {
                }finally {
                    lock.unlock();
                }
            }
        };
        executorService.submit(runnable);
    }

}
